<?php
// +----------------------------------------------------------------------
// | 针对php发送http请求的相关方法
// +----------------------------------------------------------------------

//post('mmsplat.eucp.b2m.cn','/MMSCenterInterface/MMS.asmx/SendMMS','a=asdfsdfsdf&b=kjhdsfkjhsdkjf');
/**
 * 发送post请求
 * 
 * 例子：
 * 	接口地址：http://mmsplat.eucp.b2m.cn/MMSCenterInterface/MMS.asmx
 *  POST /MMSCenterInterface/MMS.asmx/SendMMS HTTP/1.1
	Host: mmsplat.eucp.b2m.cn
	Content-Type: application/x-www-form-urlencoded
	Content-Length: length

	userName=string&password=string&title=string&userNumbers=string&MMSContent=string&MMSContent=string&sendType=string
	
 * @param String $host
 * @param String $path
 * @param String $data
 */
function k_http_post($host,$path,$data){
	return;
    $fp = fsockopen($host, 80);
    fputs($fp,"POST $path HTTP/1.1\r\n");
    fputs($fp,"Host: $host\r\n");
    fputs($fp,"Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp,"Content-length: " . strlen($data) . "\r\n");
    //fputs($fp, "User-Agent: MSIE\r\n");
    //fputs($fp, "Connection: close\r\n\");
    fputs($fp, "\r\n");
    fputs($fp, $data);
    
    $tmp_ = $tmp_."-------------------------------------------------------------------------\r\n";
    $tmp_ = $tmp_."POST $path HTTP/1.1\r\n";
    $tmp_ = $tmp_."Host: $host\r\n";
    $tmp_ = $tmp_."Content-type: application/x-www-form-urlencoded\r\n";
    $tmp_ = $tmp_."Content-length: " . strlen($data) . "\r\n";
    $tmp_ = $tmp_."\r\n";
    $tmp_ = $tmp_.$data;
    $tmp_ = $tmp_."\r\n-------------------------------------------------------------------------";
    Log::write($tmp_);
    $buf='';
    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}


// example
/**
$arr    = array(
    "a" => 1,
    "b"    => 2,
    "3"    => "AB"
);
postDataBySocket("localhost", "/t.php", "post", $arr);
**/
/**
* 函数功能：通过socket传递数据
*  	POST /MMSCenterInterface/MMS.asmx/SendMMS HTTP/1.1
	Host: mmsplat.eucp.b2m.cn
	Content-Type: application/x-www-form-urlencoded
	Content-Length: length

	userName=string&password=string&title=string&userNumbers=string&MMSContent=string&sendType=string
* @param string host        主机
* @param string filename    目标文件的路径和文件名
* @param string method        传递方式
* @param array params        传递数据格式
*        array(
*            "key" => "val"
*        );
* @param int port            目标端口
* @param int timeout       
* @param boolean debug
*
*/
function postDataBySocket($host, $filename, $method, $params, $port = 80, $timeout = 30) {

    if (!$host || !$filename || !$method || !$params)    return '';

    // 打开socket链接
    $fp    = fsockopen($host, $port, $errno, $errstr, $timeout);
    if (!$fp)    return ($debug) ? $errstr : false;

    $str = "";
    foreach ($params as $key => $val) {
        $str .= $key. "=". $val. "&";
    }
    $str    = substr($str, 0, -1);
    $method    = strtoupper($method);
    $out = "{$method} {$filename} HTTP/1.1\r\n";
    $out .= "Host: {$host}\r\n";
    $out .= "Content-type: application/x-www-form-urlencoded\r\n";
    $out .= "Content-Length: ". strlen($str) . "\r\n";
    $out .= "User-Agent: MSIE\r\n";
    $out .= "Connection: Close\r\n";
    $out .= "\r\n";
    $out .= $str."\r\n\r\n";
	
    $tmp_ = $tmp_."-------------------------------------------------------------------------\r\n";
    $tmp_ = $tmp_.$out;
    $tmp_ = $tmp_."\r\n-------------------------------------------------------------------------";
    Log::write($tmp_);
    fwrite($fp, $out);

    //if ($debug) {
    //    while (!feof($fp)) {
    //        echo fgets($fp, 128);
    //    }
    //}
	$buf='';
    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
    //fclose($fp);

    //return true;
}

?>